/**
 * @author 董一华
 * @since 1.00
 * @version 1.00
 * 
 * 根据地区(省+地市)获取部门下拉框控件
 * 使用说明：
 * jsp中使用范例(最短模式)：<htdz-tag:SelectAreaDeptTag/>
 *             (最常用模式)：<htdz-tag:SelectAreaDeptTag areaid="${xxx}" deptid="${xxx}"/>
 * 参数说明：
 * 1. 地区name为"areaid",部门name为"deptid"
 * 2. firstoption为首个下拉框的text。可以不写，缺省值为"请选择"；
 *    如果firstoption为MyConstant.SELECT_NO_FIRST_OPTION，则表示此下拉框不需要添加额外option
 * 3. disabled：是否禁用true/false，缺省值为false
 * 4. hidedesc：是否隐藏文字说明“地市：”和“部门：”，缺省值为显示
 * 5. cityonly:是否只显示一个地市，缺省值为false。当用户为地市组织时，有时只需要显示该用户所在地市即可，当为true时,selected值为必填项；
 * 
 * 修订列表： v1.00 2011-09-02 
 * 			修订人：董一华 
 * 			修订内容：初始版本
 * 
 */

package com.zjht.gmcc.bean.tag;

import java.util.Iterator;
import java.util.Map;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import com.zjht.gmcc.bean.util.MyConstant;
import com.zjht.gmcc.bean.util.MyFormat;
import com.zjht.gmcc.bean.util.MyParam;
import com.zjht.gmcc.bean.util.MyParamUtil;

public class SelectAreaDeptTag extends TagSupport {
	private static final long serialVersionUID = 7343412033252888637L;
	private String firstoption;
	private boolean disabled;
	private boolean cityonly;
	private String areaid;
	private String deptid;
	private boolean hidedesc;

	public SelectAreaDeptTag() {
	}

	@SuppressWarnings("static-access")
	public int doStartTag() throws JspException {
		String firstoption = this.firstoption;
		boolean disabled = this.disabled;
		boolean cityonly = this.cityonly;
		String areaid = this.areaid;
		String deptid = this.deptid;
		boolean hidedesc = this.hidedesc;

		StringBuilder html = new StringBuilder();
		try {
			if (!hidedesc) {
				html.append("地市：");
			}
			html.append("<select name=\"areaid\" id=\"areaid\" ");
			if (!cityonly) {
				html.append(" onchange=\"changeAreaID__AreaDeptTag(this.value)\" ");
			}
			if (disabled)// 禁用
				html.append(" disabled ");
			html.append(">");
			if (cityonly && !MyFormat.isStrNull(areaid)) {// 只显示一个地市
				html.append("<option value=\"");
				html.append(areaid);
				html.append("\">");
				html.append(MyParamUtil.getAreaName(areaid));
				html.append("</option>");
			} else {
				if (!MyConstant.SELECT_NO_FIRST_OPTION.equals(firstoption)) {// 如果firstoption为MyConstant.SELECT_NO_FIRST_OPTION，则不需要添加额外的option
					if (firstoption == null || firstoption.length() == 0)
						firstoption = "请选择";
					html.append("<option value=\"");
					html.append(MyConstant.SELECT_FIRST_VALUE);
					html.append("\">");
					html.append(firstoption);
					html.append("</option>");
				}
				html.append("<option value=\"");
				html.append(MyConstant.AREAID_GMCC);
				html.append("\">浙江移动</option>");
				if (MyParam.hAreaIDToAreaName != null && MyParam.hAreaIDToAreaName.size() > 0) {// 全部地市
					Iterator<Map.Entry<String, String>> iterator = MyParam.hAreaIDToAreaName.entrySet().iterator();
					String key = null;
					String value = null;
					Map.Entry<String, String> entry = null;
					while (iterator.hasNext()) {
						entry = iterator.next();
						key = entry.getKey();
						value = entry.getValue();
						html.append("<option value=\"");
						html.append(key);
						html.append("\" ");
						if (!MyFormat.isStrNull(areaid) && key.equals(areaid))
							html.append("selected");
						html.append(">");
						html.append(value);
						html.append("</option>");
					}
				}
			}
			html.append("</select> ");
			if (!hidedesc) {
				html.append("部门：");
			}
			html.append("<select name=\"deptid\" id=\"deptid\" ");
			if (disabled)// 禁用
				html.append(" disabled ");
			html.append(">");
			if (!MyConstant.SELECT_NO_FIRST_OPTION.equals(firstoption)) {// 如果firstoption为MyConstant.SELECT_NO_FIRST_OPTION，则不需要添加额外的option
				if (firstoption == null || firstoption.length() == 0)
					firstoption = "请选择";
				html.append("<option value=\"");
				html.append(MyConstant.SELECT_FIRST_VALUE);
				html.append("\">");
				html.append(firstoption);
				html.append("</option>");
			}
			Map<String, String> hDept = null;
			int orgtype = 0;
			int i_areaid = MyFormat.formatInt(areaid);
			if (MyFormat.checkAreaID(i_areaid)) {
				if (MyConstant.AREAID_GMCC == i_areaid) {
					orgtype = MyConstant.ORGTYPE_PROVMOBILE;
				} else {
					orgtype = MyConstant.ORGTYPE_CITYMOBILE;
				}
				hDept = MyParam.hOrgtypeDept.get(orgtype);
				if (hDept != null && hDept.size() > 0) {
					Iterator<Map.Entry<String, String>> iterator = hDept.entrySet().iterator();
					String key = null;
					String value = null;
					Map.Entry<String, String> entry = null;
					while (iterator.hasNext()) {
						entry = iterator.next();
						key = entry.getKey();
						value = entry.getValue();
						html.append("<option value=\"");
						html.append(key);
						html.append("\" ");
						if (!MyFormat.isStrNull(deptid) && key.equals(deptid))
							html.append("selected");
						html.append(">");
						html.append(value);
						html.append("</option>");
					}
				}
			}
			html.append("</select>");
			if (!cityonly) {// 显示全部地市
				html.append("<script> function changeAreaID__AreaDeptTag(areaid){clearSelect('deptid');if(areaid!='");
				html.append(MyConstant.SELECT_FIRST_VALUE);
				html.append("'){ var deptSelect=document.getElementById('deptid'); if(deptSelect!=null){if(areaid=='");
				html.append(MyConstant.AREAID_GMCC);
				html.append("'){");
				hDept = MyParam.hOrgtypeDept.get(MyConstant.ORGTYPE_PROVMOBILE);
				if (hDept != null && hDept.size() > 0) {
					Iterator<Map.Entry<String, String>> iterator = hDept.entrySet().iterator();
					String key = null;
					String value = null;
					Map.Entry<String, String> entry = null;
					while (iterator.hasNext()) {
						entry = iterator.next();
						key = entry.getKey();
						value = entry.getValue();
						html.append(" deptSelect.options[deptSelect.options.length]=new Option('");
						html.append(value);
						html.append("','");
						html.append(key);
						html.append("');");
					}
				}
				html.append("}else{");
				hDept = MyParam.hOrgtypeDept.get(MyConstant.ORGTYPE_CITYMOBILE);
				if (hDept != null && hDept.size() > 0) {
					Iterator<Map.Entry<String, String>> iterator = hDept.entrySet().iterator();
					String key = null;
					String value = null;
					Map.Entry<String, String> entry = null;
					while (iterator.hasNext()) {
						entry = iterator.next();
						key = entry.getKey();
						value = entry.getValue();
						html.append(" deptSelect.options[deptSelect.options.length]=new Option('");
						html.append(value);
						html.append("','");
						html.append(key);
						html.append("');");
					}
				}
				html.append("}}}}</script>");
			}

			pageContext.getOut().print(html.toString());
		} catch (Exception e) {
			throw new JspException(e.getMessage());
		}
		html = null;
		return this.SKIP_BODY;
	}

	public void setAreaid(String areaid) {
		this.areaid = areaid;
	}

	public void setAreaid(int areaid) {
		this.areaid = String.valueOf(areaid);
	}

	public void setDeptid(String deptid) {
		this.deptid = deptid;
	}

	public void setDeptid(int deptid) {
		this.deptid = String.valueOf(deptid);
	}

	public void setFirstoption(String firstoption) {
		this.firstoption = firstoption;
	}

	public void setDisabled(boolean disabled) {
		this.disabled = disabled;
	}

	public void setDisabled(String disabled) {
		this.disabled = (disabled != null && "true".equals(disabled.trim().toLowerCase()));
	}

	public void setCityonly(boolean cityonly) {
		this.cityonly = cityonly;
	}

	public void setCityonly(String cityonly) {
		this.cityonly = (cityonly != null && "true".equals(cityonly.trim().toLowerCase()));
	}

	public void setHidedesc(boolean hidedesc) {
		this.hidedesc = hidedesc;
	}
}
